import Vue from 'vue'
import Vuex from 'vuex'
import router from './router'
import {
    _getcyOrder,
    _getgzOrder,
    _getjyOrder,
    _getUserInfo,
    _getProConf,
    _hadSetBundPw
} from "@/axios/index.js";

const vueDemo = new Vue();
Vue.use(Vuex);
let store = new Vuex.Store({
    state: {
        token: '',
        userMoney:0,//用户可用余额
        user: "", //用户数据
        symbolTypeList: '',
        symbolList: '',
        symbolObj: {},
        blastingWarehouse: 0, //爆仓判断
        isLogin: false,
        traViewEvent: {}, //TV事件
        traViewSymboId: 'wh_eurusd', //当前图表库商品
        stockList: null, //股市列表详情数据。
        stockSymBol: null, // 股市当前图表库商品
        announcemState: null,
        ccDataList: "", //持仓数据 
        gdDataList: '', //挂单数据
        ddDataList: '', //订单数据
        pricescale: { num: 10000 }, //图表小数
        newPrice: null,
        detialTab: null, //交易页当前分类
        isZJPWD: '未设置', //是否设置了资金密码
        confObj: {
            risk_warning: "", //风险提示
            privacy_policy: '', //隐私条款
            disclaimer: '', //免责声明 
            registration_agreement: "" ,//注册协议
            service_img:""
        }

    },
    mutations: {
        //交易页商品分类
        setdetialTab(state, type_id) {
            switch (type_id) {
                case 1:
                    state.detialTab = '外汇';
                    break;
                case 2:
                    state.detialTab = '商品'
                    break
                case 3:
                    state.detialTab = '指数'
                    break
                case 4:
                    state.detialTab = '数字货币'
                    break
            }
        },
        setCcDataList(state, obj) {
            state.ccDataList = obj;
        },
        setGdDataList(state, obj) {
            state.gdDataList = obj;
        },
        setDdDataList(state, obj) {
            state.ddDataList = obj;
        },
        setToken(state, token) {
            state.token = token;
            localStorage.setItem('token', token);
        },
        setIsLogin(state, bol) {
            state.isLogin = bol;
        },
        setUser(state, user) {
            state.user = user;
        },
        setBlastingWarehouse(state, bol) {
            state.blastingWarehouse = bol;
        },
        cleartloding(state, path = null) {
            state.token = '';
            state.user = '';
            state.ccDataList = "";
            state.gdDataList = "";
            state.ddDataList = "";
            state.isLogin = false;
            localStorage.removeItem('token')
            if (path != null) {
                router.push(path)
            }
        },
    },
    getters: {
        //持仓
        countCcDataList(state) {
            if (!state.ccDataList) {
                store.dispatch("getCcDataList")
            }
        },
        //挂单
        countGdDataList(state) {
            if (!state.gdDataList) {
                store.dispatch("getGdDataList")
            }
        },
        //订单
        countDdDataList(state) {
            if (!state.ddDataList) {
                store.dispatch("getddDataList")
            }
        },
        //风险提示、隐私条款、免责声明
        countConf: (state) => (cur) => {
            if (!state.confObj[cur]) {
                store.dispatch("getConf", cur);
            }
        }



    },
    actions: {
    
        //获取持仓数据
        async getCcDataList(store) {
            const res = await _getcyOrder({
                uid: store.state.user.id
            });

            if (res.data.code == 200) {
                // store.commit("setCcDataList",res.data.data);
                store.state.ccDataList = res.data.data;
            }
        },
        //获取挂单数据
        async getGdDataList(store) {
            const res = await _getgzOrder({
                uid: store.state.user.id
            });
            if (res.data.code == 200) {
                //   store.commit("setGdDataList",res.data.data);
                store.state.gdDataList = res.data.data;
            }
        },
        //获取订单第一页数据
        async getddDataList(store) {
            const res = await _getjyOrder({
                uid: store.state.user.id,
                page: 1
            });
            if (res.data.code == 200) {
                store.commit("setDdDataList", res.data.data);
            }
        },
        //获取用户信息
        async getUser(store) {
            const res = await _getUserInfo({ user_type: 1 });

            if (res.data.code == 200) {
                store.commit("setUser", res.data.data);
            }
        },
        //获取是否设置了资金密码
        async getIsZJPWD() {
            const res = await _hadSetBundPw(store.state.user.user_type);
            if (res.data.code === 200) {
                store.state.isZJPWD = res.data.msg;
            }
        },
        //风险提示、隐私条款、免责声明
        async getConf(store, products) {
            const res = await _getProConf(products);
            if (res.data.code == 200) {
                store.state.confObj[products] = res.data.data;
            }
        },
    }
});

if (localStorage.getItem('pricescale') != '') {
    store.state.pricescale.num = localStorage.getItem('pricescale')
}

export default store;